home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / basic / _basic.c.ORG < prev    next >
Encoding:
Text File  |  1994-11-16  |  3.6 KB  |  208 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _basic.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/basic.h>
  16.  
  17. #include <time.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20.  
  21. #if defined(__MSDOS__)
  22. #include <dos.h>
  23. #else
  24. #include <unistd.h>
  25. #include <sys/times.h>
  26. #include <sys/param.h>
  27. #endif
  28.  
  29.  
  30. int LEDA::loop_dummy = 0;
  31.  
  32.  
  33. LEDA::LEDA()
  34. { init_list = getenv("LEDA_INIT");
  35.   init_random();
  36.  }
  37.  
  38.  
  39. LEDA::~LEDA() 
  40.   if (init_list && strcmp(init_list,"statistics")==0)
  41.   { if (version_string) printf("\n%s\n",version_string);
  42.     printf("\n");
  43.     print_statistics(); 
  44.    }
  45.  } 
  46.  
  47.  
  48. LEDA L_E_D_A;
  49.  
  50.  
  51.  
  52. //------------------------------------------------------------------------------
  53. // Error Handling
  54. //------------------------------------------------------------------------------
  55.  
  56.  
  57. PEH p_error_handler = default_error_handler;
  58.  
  59. void default_error_handler(int i, const char* s)
  60. { if (i==0) 
  61.    cout << "(warning) " << s << "\n";
  62.   else 
  63.    { cout << "ERROR "<< s << "\n";
  64.      cout.flush();
  65.      abort();
  66.     }
  67. }
  68.  
  69. PEH set_error_handler(PEH handler)
  70. { PEH old = error_handler;
  71.   p_error_handler = handler;
  72.   return old;
  73. }
  74.  
  75. //------------------------------------------------------------------------------
  76. // useful functions
  77. //------------------------------------------------------------------------------
  78.  
  79. LEDA_SIG_PF catch_interrupts(LEDA_SIG_PF handler) 
  80. { error_handler(0,"catch_interrupts not implemented.");
  81.   return handler; 
  82.  }
  83.  
  84.  
  85.  
  86. void init_random(int seed)
  87. { time_t l = seed;
  88.   if (l==0) time(&l);
  89.   srandom(int(l));
  90.  }
  91.  
  92.  
  93. #if defined(__MSDOS__) 
  94.  
  95. #if defined(__GNUG__)
  96. static long clock()
  97. { union REGS regs;
  98.   regs.h.ah=0x00;
  99.   int86(0x1A,®s,®s);
  100.   return (regs.x.cx << 16) + regs.x.dx;
  101. }
  102. #define CLK_TCK  18.2
  103. #endif
  104.  
  105. float used_time() { return  float(clock())/CLK_TCK; }
  106.  
  107. #else
  108.  
  109. float used_time()
  110. { tms x;
  111.   times(&x);
  112.   return  float(x.tms_utime)/HZ;
  113. }
  114.  
  115. #endif
  116.  
  117.  
  118. float used_time(float& T)
  119. { float t = T;
  120.   T =  used_time();
  121.   return  T-t;
  122. }
  123.  
  124.  
  125. void print_time(string s)
  126. { cout << s;
  127.   cout << string("  time= %.2f sec",used_time());
  128.   newline;
  129. }
  130.  
  131.  
  132. void wait(unsigned int sec) { sleep(sec); }
  133.   
  134.  
  135. //------------------------------------------------------------------------------
  136. // Input/Ouput
  137. //------------------------------------------------------------------------------
  138.  
  139.  
  140. int Yes(string s)
  141. { char answer = read_char(s);
  142.   return ((answer == 'y') || (answer == 'Y'));
  143. }
  144.  
  145. int read_int(string s)
  146. { int answer;
  147.   char c;
  148.  
  149.   for(;;)
  150.   { cout << s;
  151.     cin >> answer;
  152.     if (!cin) 
  153.       { cin.clear();
  154.         cin.get(c);
  155.         cout << string("read_int: illegal input \"%c\"\n",c);
  156.         if (c != '\n') skip_line(cin);
  157.        }
  158.     else  
  159.        break;
  160.    }
  161.  
  162.   skip_line(cin);
  163.  
  164.   return answer;
  165. }
  166.  
  167. char read_char(string s)
  168. { char c;
  169.   cout << s;
  170.   cin.get(c);
  171.   if (c != '\n') skip_line(cin);
  172.   return c;
  173. }
  174.  
  175. double read_real(string s)
  176. { double answer;
  177.   cout << s;
  178.   cin >> answer;
  179.   skip_line(cin);
  180.   return answer;
  181. }
  182.  
  183. string read_line(istream& in)
  184. { string result;
  185.   result.read_line(in); 
  186.   return result;
  187.  }
  188.  
  189. string read_string(string s)
  190. { cout << s << flush;
  191.   return read_line(cin); 
  192.  }
  193.  
  194. void skip_line(istream& s)
  195. { char c;
  196.   while(s.get(c) &&  c != '\n');
  197.  }
  198.  
  199.  
  200. int     Yes()              { return Yes(""); }
  201. int     read_int()         { return read_int(""); }
  202. char    read_char()        { return read_char(""); }
  203. double  read_real()        { return read_real(""); }
  204. string  read_string()      { return read_string(""); } 
  205.  
  206.